package com.ruoyi.logistic.utils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 腾讯云
 */
@Component
public class SMSTencentUtils {
    //API密钥secretId
    private static String secretId;
    //API密钥secretKey
    private static String secretKey;
    // appId
    private static String appId;
    // 短信模板id
    private static String templateId;

    @Value("${smsTencentUconfig.secretId}")
    private String SecretId;
    @Value("${smsTencentUconfig.secretKey}")
    private String SecretKey;
    @Value("${smsTencentUconfig.appId}")
    private String AppId;
    @Value("${smsTencentUconfig.templateId}")
    private String TemplateId;

    @PostConstruct
    public void transValues() {
        secretId = this.SecretId;
        secretKey = this.SecretKey;
        appId = this.AppId;
        templateId = this.TemplateId;
    }

    /**
     * 发送短信
     *
     * @param phone  手机号
     * @param params 模板参数，若无模板参数，则设置为空。  [前缀，验证码，时间]
     * @return
     */
    public static SendStatus sendSms(String phone, String[] params) throws TencentCloudSDKException {
        // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
        // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);

        // 实例化一个请求对象,每个接口都会对应一个request对象
        SendSmsRequest req = new SendSmsRequest();
        // +86 代表国内
        phone = "+86" + phone;
        String[] phoneNumberSet = {phone};
        req.setPhoneNumberSet(phoneNumberSet);
        req.setSmsSdkAppid(appId);
        req.setTemplateID(templateId);
        req.setTemplateParamSet(params);
        // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
        SendSmsResponse resp = client.SendSms(req);
        // 输出json格式的字符串回包
        SendStatus[] sendStatusSet = resp.getSendStatusSet();
        SendStatus result = sendStatusSet[0];
        /*if (null != result) {
            String code = result.getCode();
            if ("Ok".equals(code)) {
                return true;
            }
        }
        return false;*/
        return result;
    }
}
